/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.entity.MethodQos;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class MethodQosPanel extends Composite {
	private ListBox commType;
	private TextBox timeout, warnErrorHour, warnErrorTime, warnErrorEmail,
			warnResHour, warnResSeconds, warnResEmail;
	private CheckBox warn, errorEnable, resEnable;
	private FlexTable table;
	private ServiceMessages cons;
	private VerticalPanel h1, h2;

	public MethodQosPanel(ServiceMessages c, ArrayList qos) {
		this.cons = c;
		
		HashMap<String,String> tmp = new HashMap<String,String>();
		if (qos != null) {
			
			for (int i = 0, j = qos.size(); i < j; i++) {
				MethodQos q = (MethodQos) qos.get(i);
				tmp.put(q.getName(), q.getValue());

			}
		}
		table = new FlexTable();
		table.setWidth("100%");
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);

		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "30%");
		cf.setWidth(1, "70%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");

		// ff.setStyleName(0, 0, "tablecol");
		table.setHTML(0, 0, cons.commType());
		commType = new ListBox();

		commType.addItem(cons.sync(), EntityConstants.SYNC);
		commType.addItem(cons.asyncWithReply(),
				EntityConstants.ASYNC_WITH_REPLY);
		commType.setSelectedIndex(0);
		
		if(!StringUtils.isEmpty(tmp.get(EntityConstants.QOS_COMM_TYPE)) && tmp.get(EntityConstants.QOS_COMM_TYPE).equals(EntityConstants.ASYNC_WITH_REPLY)){
			commType.setSelectedIndex(1);
		}
		table.setWidget(0, 1, commType);

		ff.setStyleName(1, 0, "tablecol");
		table.setHTML(1, 0, cons.timeout());
		timeout = new TextBox();
		timeout.setVisibleLength(4);
		timeout.setMaxLength(4);
		timeout.setText("60");
		table.setWidget(1, 1, timeout);
		String time = tmp.get(EntityConstants.QOS_TIMEOUT);
		if(! StringUtils.isEmpty(time))
			timeout.setText(time);

		int row = table.getRowCount();
		table.insertRow(row);
		ff.setStyleName(row, 0, "tablecol");
		table.setHTML(row, 0, cons.warn());
		warn = new CheckBox();
		table.setWidget(row, 1, warn);

		h1 = new VerticalPanel();

		errorEnable = new CheckBox(cons.enable());
		h1.add(errorEnable);

		h1.add(new HTML("&nbsp;"));

		HorizontalPanel h = new HorizontalPanel();
		h.add(new HTML(cons.warnError1() + "&nbsp;"));
		warnErrorHour = new TextBox();
		warnErrorHour.setMaxLength(4);
		warnErrorHour.setVisibleLength(4);
		h.add(warnErrorHour);
		h.add(new HTML("&nbsp;" + cons.warnError2()));
		h.add(new HTML(cons.warnError3() + "&nbsp;"));
		warnErrorTime = new TextBox();
		warnErrorTime.setMaxLength(4);
		warnErrorTime.setVisibleLength(4);
		h.add(warnErrorTime);
		h.add(new HTML("&nbsp;" + cons.warnError4()));

		h1.add(h);

		h = new HorizontalPanel();
		h.add(new HTML(cons.warnError5() + "&nbsp;"));

		warnErrorEmail = new TextBox();
		// warnErrorEmail.setMaxLength(4);
		warnErrorEmail.setVisibleLength(16);
		h.add(warnErrorEmail);
		h1.add(h);

		h2 = new VerticalPanel();

		resEnable = new CheckBox(cons.enable());
		h2.add(resEnable);
		h2.add(new HTML("&nbsp;"));

		h = new HorizontalPanel();
		h.add(new HTML(cons.warnResTime1() + "&nbsp;"));
		warnResHour = new TextBox();
		warnResHour.setMaxLength(4);
		warnResHour.setVisibleLength(4);
		h.add(warnResHour);
		h.add(new HTML("&nbsp;" + cons.warnResTime2()));

		h.add(new HTML("&nbsp;" + cons.warnResTime3()));

		h2.add(h);

		h = new HorizontalPanel();
		h.add(new HTML(cons.warnResTime4() + "&nbsp;"));
		warnResSeconds = new TextBox();
		warnResSeconds.setMaxLength(4);
		warnResSeconds.setVisibleLength(4);
		h.add(warnResSeconds);
		h.add(new HTML("&nbsp;" + cons.warnResTime5()));

		h.add(new HTML("&nbsp;" + cons.warnResTime6() + "&nbsp;"));
		warnResEmail = new TextBox();
		// warnErrorEmail.setMaxLength(4);
		warnResEmail.setVisibleLength(16);
		h.add(warnResEmail);
		h2.add(h);
		warn.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				CheckBox b = (CheckBox) sender;
				if (b.isChecked()) {
					int row = table.getRowCount();

					table.insertRow(row);
					CellFormatter ff = table.getCellFormatter();
					ff.setStyleName(row, 0, "tablecol");
					table.setHTML(row, 0, cons.warnError());
					table.setWidget(row, 1, h1);

					table.insertRow(row + 1);
					ff.setStyleName(row + 1, 0, "tablecol");
					table.setHTML(row + 1, 0, cons.warnResTime());
					table.setWidget(row + 1, 1, h2);

				} else {
					int row = table.getRowCount();
					table.removeRow(row - 1);
					table.removeRow(row - 2);
				}

			}
		});
		
		if(tmp.get(EntityConstants.QOS_WARN_ERROR) != null ||
				tmp.get(EntityConstants.QOS_WARN_RESPONSE_TIME) != null){
			warn.setChecked(true);
			table.insertRow(row);
			ff = table.getCellFormatter();
			ff.setStyleName(row, 0, "tablecol");
			table.setHTML(row, 0, cons.warnError());
			table.setWidget(row, 1, h1);

			table.insertRow(row + 1);
			ff.setStyleName(row + 1, 0, "tablecol");
			table.setHTML(row + 1, 0, cons.warnResTime());
			table.setWidget(row + 1, 1, h2);
			String errorXml = tmp.get(EntityConstants.QOS_WARN_ERROR);
			if(! StringUtils.isEmpty(errorXml)){
				errorEnable.setChecked(true);
				String[] params = QosUtil.getErrorXmlParams(errorXml);
				warnErrorHour.setText(params[0]);
				warnErrorTime.setText(params[1]);
				warnErrorEmail.setText(params[2]);
			}
			String resXml = tmp.get(EntityConstants.QOS_WARN_RESPONSE_TIME);
			if(! StringUtils.isEmpty(resXml)){
				resEnable.setChecked(true);
				String[] params = QosUtil.getResQosXmlParam(resXml);
				warnResHour.setText(params[0]);
				warnResSeconds.setText(params[1]);
				warnResEmail.setText(params[2]);
			}
		}
		initWidget(table);

	}

	public ArrayList<MethodQos> getMethodQos() {

		ArrayList<MethodQos> ret = new ArrayList<MethodQos>();
		MethodQos q = new MethodQos();

		q = new MethodQos();
		q.setName(EntityConstants.QOS_COMM_TYPE);
		q.setValue(commType.getValue(commType.getSelectedIndex()));
		ret.add(q);

		if (!StringUtils.isInteger(timeout.getText())) {
			Window.alert(cons.mustBePositiveIntError(cons.timeout()));
			return null;

		}
		q = new MethodQos();
		q.setName(EntityConstants.QOS_TIMEOUT);
		q.setValue(timeout.getText());
		ret.add(q);

		if (warn.isChecked()) {
			if (resEnable.isChecked()) {
				if (!StringUtils.isInteger(warnResHour.getText())) {
					Window.alert(cons.mustBePositiveIntError(cons.hour()));
					return null;
				}
				if (!StringUtils.isInteger(warnResSeconds.getText())) {
					Window
							.alert(cons.mustBePositiveIntError(cons
									.resSeconds()));
					return null;
				}
				if (!StringUtils.isEmail(warnResEmail.getText())) {
					Window.alert(cons.emailFormatError(cons.email()));
					return null;
				}
				q = new MethodQos();
				q.setName(EntityConstants.QOS_WARN_RESPONSE_TIME);
				q.setValue(QosUtil.getResQosStr(warnResHour.getText(),
						warnResSeconds.getText(), warnResEmail.getText()));
				ret.add(q);
			}

			if (errorEnable.isChecked()) {
				if (!StringUtils.isInteger(warnErrorHour.getText())) {
					Window.alert(cons.mustBePositiveIntError(cons.hour()));
					return null;
				}
				if (!StringUtils.isInteger(warnErrorTime.getText())) {
					Window.alert(cons.mustBePositiveIntError(cons.times()));
					return null;
				}
				if (!StringUtils.isEmail(warnErrorEmail.getText())) {
					Window.alert(cons.emailFormatError(cons.email()));
					return null;
				}
				q = new MethodQos();
				q.setName(EntityConstants.QOS_WARN_ERROR);
				q.setValue(QosUtil.getErrorQosStr(warnErrorHour.getText(),
						warnErrorTime.getText(), warnErrorEmail.getText()));
				ret.add(q);
			}
		}
		return ret;

	}
}
